//消失的两个数字
//测试链接 https://leetcode.cn/problems/missing-two-lcci/
public class MissingTwo {
    public int[] missingTwo(int[] nums) {
        int n = nums.length;
        int sum = 0;
        for(int i = 0; i < n; i++){ //异或上数组中所有数
            sum = sum ^ nums[i];
        }
        for(int i = 1; i <= n + 2; i++){ //在异或上1~n+2
            sum ^= i;
        }
        //此时sum的结果为缺失的两个数相异或

        int rightone = sum & (-sum);  //找到sum最左边的1；

        int[] ans = new int[2];
        for(int i = 1; i <= n+ 2; i++){
            if((rightone & i) == 0){
                ans[0] ^= i;
            }else{
                ans[1] ^= i;
            }
        }
        for(int i = 0; i<n; i++){
            if((rightone & nums[i]) == 0){
                ans[0] ^= nums[i];
            }else{
                ans[1] ^= nums[i];
            }
        }

        return ans;
    }
}
